<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :style="cardStyle" :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <!-- 搜索区域 -->
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="6" :sm="24">
                <a-form-item label="二级审批状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <!-- <a-input placeholder="请选择" v-model="queryParam.statusB"></a-input> -->
                  <a-select
                    v-model="queryParam.statusB"
                    allowClear
                    placeholder="请选择"
                    showSearch
                    optionFilterProp="children"
                  >
                    <a-select-option v-for="(item, index) in status" :key="index" :value="item.id">
                      {{ item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="内部合同编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input placeholder="请输入内部合同编码查询" v-model="queryParam.contractCode"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="合同号" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input placeholder="请输入合同号查询" v-model="queryParam.contractNum"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input placeholder="请输入客户名称查询" v-model="queryParam.customerName"></a-input>
                </a-form-item>
              </a-col>
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-col :md="6" :sm="24">
                  <a-button type="primary" @click="searchQuery">查询</a-button>
                  <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
                </a-col>
              </span>
            </a-row>
          </a-form>
        </div>
        <div class="table-operator" style="margin: 5px 0px">
          <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
        </div>
        <div>
          <a-table
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            @change="handleTableChange"
          >
            <span slot="action" slot-scope="text, record">
              <a @click="ActionApproval(record)">审批</a>
              <a-divider type="vertical" />
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a>删除</a>
              </a-popconfirm>
            </span>
            <span slot="needPurchase" slot-scope="text, record">
              <div>{{ record.needPurchase == 0 ? '不需要' : '需要' }}</div>
            </span>
            <span slot="haveBlueprint" slot-scope="text, record">
              <a-tag color="cyan" @click="toUpload(record)">
                <a>{{ record.haveBlueprint == 0 ? '无' : '有' }}</a>
              </a-tag>
            </span>
            <span slot="statusB" slot-scope="text, record">
              <div>{{ record.statusB == 0 ? '待审批' : record.statusB == 1 ? '通过' : '驳回' }}</div>
            </span>
            <span slot="stageB" slot-scope="text, record">
              <a @click="printPage(record)" style=" color:#87d068" v-if="record.stageB == 5">审批完成</a>
              <div v-else>
                <div v-for="(item, index) in stageList" :key="index">
                  {{ record.stageB == item.id ? item.name : '' }}
                </div>
              </div>
            </span>
            <span slot="contractFile" slot-scope="text, record">
              <a @click="myhandleExportXls(record.contractFile)">下载</a>
              <a-divider type="vertical" />
              <a @click="previewFile(record.contractFile)">预览</a>
            </span>
            <span slot="blueprintUrl" slot-scope="text, record">
              <a @click="myhandleExportXls(record.blueprintUrl)">下载</a>
              <a-divider type="vertical" />
              <a @click="previewFile(record.blueprintUrl)">预览</a>
            </span>
          </a-table>
        </div>
        <approvalModel ref="modalForm" @ok="modalFormOk"></approvalModel>
        <PrintPage ref="PrintPageModel" @ok="modalFormOk"></PrintPage>
        <ToUpload ref="ToUploadModel" @ok="modalFormOk"></ToUpload>
        <ActionTwoApproval ref="ActionTwoApprovalModel" @ok="modalFormOk"></ActionTwoApproval>
      </a-card>
    </a-col>
  </a-row>
</template>
<script>
import { getActionImg, downFile, getAction, deleteAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import approvalModel from './modules/ApprovalModels.vue'
import ToUpload from './modules/ToUpload.vue'
import ActionApprovalModel from './modules/ActionApprovalModel.vue'
import ActionTwoApproval from './modules/ActionTwoApproval.vue'
import PrintPage from './modules/PrintPage.vue'
export default {
  name: 'secondApproval',
  mixins: [JeecgListMixin],
  components: { approvalModel, ActionApprovalModel, PrintPage, ToUpload, ActionTwoApproval },
  data() {
    return {
      dataSource: [],
      stageList: [
        { id: 0, name: '待审批' },
        { id: 1, name: '销售部' },
        { id: 2, name: '生产部' },
        { id: 3, name: '评审结论' },
        { id: 4, name: '审批意见' },
        { id: 5, name: '审批完成' }
      ],
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 40,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        { title: '内部编码', dataIndex: 'contractCode', width: 120, align: 'center' },
        { title: '合同号', dataIndex: 'contractNum', width: 120, align: 'center' },
        { title: '申请人', dataIndex: 'applicant', width: 100, align: 'center' },
        { title: '客户名称', dataIndex: 'customerName', width: 100, align: 'center' },
        {
          title: '二级审批流程',
          dataIndex: 'stageB',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'stageB' }
        },
        {
          title: '二级审批状态',
          dataIndex: 'statusB',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'statusB' }
        },
        {
          title: '是否需要采购',
          dataIndex: 'needPurchase',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'needPurchase' }
        },
        {
          title: '合同表',
          dataIndex: 'contractFile',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'contractFile' }
        },
        {
          title: '有无图纸表(可编辑)',
          dataIndex: 'haveBlueprint',
          width: 120,
          align: 'center',
          scopedSlots: { customRender: 'haveBlueprint' }
        },
        {
          title: '图纸',
          dataIndex: 'blueprintUrl',
          width: 100,
          align: 'center',
          scopedSlots: { customRender: 'blueprintUrl' }
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 120,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      status: [
        {
          id: 0,
          name: '待审批'
        },
        {
          id: 1,
          name: '通过'
        },
        {
          id: 2,
          name: '驳回'
        }
      ],
      labelCol: {
        span: 5
      },
      wrapperCol: {
        span: 18,
        offset: 1
      },
      url: {
        list: '/contractApproval/getApprovalPage',
        exportXlsUrl: '/systemConfig/static',
        delete: '/contractApproval/remove'
      }
    }
  },
  mounted() {},
  methods: {
    //打开上传图纸
    toUpload(record) {
      this.$refs.ToUploadModel.edit(record)
    },
    //打开打印页面
    printPage(record) {
      this.$refs.PrintPageModel.isReadOnly = true
      this.$refs.PrintPageModel.handleOpen(record)
    },
    //审批按钮
    ActionApproval(record) {
      this.$refs.ActionTwoApprovalModel.edit(record)
      this.$refs.ActionTwoApprovalModel.title = '审批'
      if (record.statusB !== 0) {
        this.$refs.ActionTwoApprovalModel.disableSubmit = true
      } else {
        this.$refs.ActionTwoApprovalModel.disableSubmit = false
      }
      if (record.stageB == 8) {
        this.$refs.ActionTwoApprovalModel.PrintPageModel = true
      } else {
        this.$refs.ActionTwoApprovalModel.isReadOnly = false
      }
    },
    //文件下载
    myhandleExportXls(getUrl) {
      if (!getUrl) {
        this.$message.warning('合同附件未上传')
      }
      downFile(this.url.exportXlsUrl + '/' + getUrl).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), getUrl)
        } else {
          let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', getUrl)
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) //下载完成移除元素
          window.URL.revokeObjectURL(url) //释放掉blob对象
        }
      })
    },
    //PDF预览
    previewFile(record) {
      if (record) {
        let params = record
        getActionImg('systemConfig/preview/' + params).then(res => {
          const binaryData = []
          binaryData.push(res)
          window.open(window.URL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' })))
        })
      } else {
        this.$message.warning('附件未上传')
      }
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
